#include <stdio.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <string.h>
#include"play_data.h"

int main(int argc, char* argv[])
{
    xmlDocPtr doc;           //定义解析文件指针
    xmlNodePtr curNode;      //定义结点指针(你需要他为了在各个结点间移动) 
    xmlChar *szKey;          //临时字符串变量
    char *szDocName = "1.xml";
    int N=0;
    int i=0;
    doc = xmlReadFile(szDocName,"GB2312",XML_PARSE_RECOVER); //解析文件
    if (NULL == doc) 
    {  
       fprintf(stderr,"Document not parsed successfully. \n");     
       return -1; 
    } 
    curNode = xmlDocGetRootElement(doc); //确定文件根元素
    /*检查确认当前文件中包含内容*/ 
    if (NULL == curNode)
    { 
       fprintf(stderr,"empty document\n"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    /*在这个例子中，我们需要确认文件是正确的类型。“root”是在这个示例中使用文件的根类型。*/
    if (xmlStrcmp(curNode->name, BAD_CAST "root")) 
    {
       fprintf(stderr,"document of the wrong type, root node != root"); 
       xmlFreeDoc(doc); 
       return -1; 
    } 
    curNode = curNode->xmlChildrenNode;

    szKey=xmlNodeGetContent(curNode);
    N=atoi(reinterpret_cast<const char *>(szKey));
    printf("%d\n",N);

    play_data_rev *data=new play_data_rev[N];
    while(curNode->next!= NULL) 
    {
       curNode = curNode->next;
      if (xmlHasProp(curNode,BAD_CAST "mediatype"))
       {
           data[i].mediatype=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "mediatype")));
       }
      printf("%d\n",data[i].mediatype);
      if (xmlHasProp(curNode,BAD_CAST "location"))
       {
           data[i].location=reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "location"));
       }
      printf("%s\n",data[i].location.c_str());
      if (xmlHasProp(curNode,BAD_CAST "playtime"))
       {
           data[i].playtime=atoi(reinterpret_cast<const char *>(xmlGetProp(curNode,BAD_CAST "playtime")));
       }
      printf("%d\n",data[i].playtime);

       i++; 
    } 
    
    xmlFreeDoc(doc);
    return 0;
}
